package DataStructure.peixu;

public class Shell {
    public static void sort(Comparable[] a){
        //1根据长度确定h
        int h=1;
        while (h< a.length/2){
            h=2*h+1;
        }
        //希尔排序
        while (h>=1){
            //找到待插入的元素
            for (int i = h; i < a.length; i++) {
                //找到待插入的元素插入到有序数列中
                for (int j = i-h; j >=0 ; j=j-h) {
                    if(greater(a[j],a[j+h])){
                        exch(a,j,j+h);
                    }else {
                        break;
                    }
                }
            }
            h=h/2;
        }
    }

    private static boolean greater(Comparable v,Comparable w){return v.compareTo(w)>0;}

    private static void exch(Comparable[] a,int i,int j){
        Comparable temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }
}
